﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.IO;
using System.Net;

using EHTWebsite.Code;
using EHTWebsiteProject.Code.BLL;
using System.Resources;
using System.Threading;
using System.Globalization;
using System.Text;
using Telerik.Web.UI;
using System.Collections.Generic;
using EHTWebsiteProject.Code.Entity;

namespace EHTWebsiteProject.Administrator
{
    public partial class EHTCategoryNews : System.Web.UI.Page
    {
        public Boolean IsVN = true;
        private Int32 danhmucID = 0;
        private String languageCode = "vi-VN";
        private ResourceManager rm = new ResourceManager("Resources.SiteMap", System.Reflection.Assembly.Load("App_GlobalResources"));
        private String Search = "";
        private String TenDanhMucTinTucVN = "";
        private String TenDanhMucTinTucOther = "";
        private DataTable dtProductCategorys = new DataTable();


        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                AjaxPro.Utility.RegisterTypeForAjax(typeof(EHTWebsiteProject.Administrator.EHTCategoryNews));
                dtProductCategorys = DanhMucTinTucProvider.GetDanhMucTinTuc().Tables[0];

                try
                {
                    languageCode = Session[HangSo.LANGUAGE_CODE].ToString();
                    if (languageCode == "en-us")
                    {
                        IsVN = false;
                    }

                }
                catch (Exception ex)
                {
                }
                try
                {
                    danhmucID = Int32.Parse(Request.QueryString["DanhMucID"]);
                }
                catch
                {

                }

                try
                {
                    Search = Request.QueryString["txtSearch"].ToString();
                }
                catch
                {

                }
                Thread.CurrentThread.CurrentCulture = new CultureInfo(languageCode);
                LoadLanguage(Thread.CurrentThread.CurrentCulture);
                RadTreeList1.ExpandToLevel(1);
                RadTreeList1.ExpandToLevel(2);

                RadTreeList2.ExpandToLevel(1);
                RadTreeList2.ExpandToLevel(2);
                
                CreateDDLCategory(0, 0);
                if (IsVN == true)
                {
                    this.ddlDanhmucSanPham.Items.Insert(0, new ListItem(HangSo.DanhMuc, "0"));
                    pnNhomTinOther.Visible = false;
                }
                else
                {
                    this.ddlDanhmucSanPham.Items.Insert(0, new ListItem(HangSo.DanhMucOther, "0"));
                    pnNhomTin.Visible = false;
                }
                ddlDanhmucSanPham.SelectedValue = danhmucID.ToString();
            }
        }

        /// <summary>
        /// Ham load danh muc san pham
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void CreateDDLCategory(Int32 ChuyenMucChaID, Int32 level)
        {
            String preString = "";

            for (Int32 i = 0; i < level; ++i)
            {
                preString += "--";
            }

            DataRow[] drCategorysSub = dtProductCategorys.Select("ChuyenMucChaID = " + ChuyenMucChaID.ToString());
            Int32 amountOfCategorySub = drCategorysSub.Count();

            //Điểm dừng đệ quy
            if (amountOfCategorySub == 0)
                return;

            for (Int32 i = 0; i < amountOfCategorySub; ++i)
            {
                Int32 subCategoryID = Int32.Parse(drCategorysSub[i]["DanhMucTinTucID"].ToString());
                ListItem li;
                if (Session[HangSo.LANGUAGE_CODE].ToString()=="vi-VN")
                    li = new ListItem(preString + drCategorysSub[i]["TenDanhMucTinTucVN"].ToString(), drCategorysSub[i]["DanhMucTinTucID"].ToString());
                else
                    li = new ListItem(preString + drCategorysSub[i]["TenDanhMucTinTucOther"].ToString(), drCategorysSub[i]["DanhMucTinTucID"].ToString());
                this.ddlDanhmucSanPham.Items.Add(li);
                CreateDDLCategory(subCategoryID, level + 1);
            }
        }


        /// <summary>
        /// Ham load ngon ngu
        /// </summary>
        /// <param name="ci"></param>
        public void LoadLanguage(CultureInfo ci)
        {
            lbAddNew.Text = rm.GetString("lbAddNew", ci);
            lbDel.Text = rm.GetString("lbDel", ci);
            lbEdit.Text = rm.GetString("lbEdit", ci);
            lbHelp.Text = rm.GetString("lbHelp", ci);
            lbCategoryNewsManager.Text = rm.GetString("lbCategoryNewsManager", ci);
            lbBat.Text = rm.GetString("lbBat", ci);
            lbTat.Text = rm.GetString("lbTat", ci);

        }

        protected void ImaTat_Click(object sender, ImageClickEventArgs e)
        {
            String IconTrangThai = "~//images//Close.png";
            Int32 Count = 0;
            if (Session[HangSo.LANGUAGE_CODE].ToString() == "vi-VN")
            {
                foreach (TreeListDataItem item in RadTreeList1.SelectedItems)
                {
                    String str = item["DanhMucTinTucID"].Text;
                    if (item.Selected == true)
                    {
                        DanhMucTinTucProvider.changePublish(Int32.Parse(str), false, IconTrangThai);
                    }
                    Count++;
                }
            }
            else
            {

                foreach (TreeListDataItem item in RadTreeList2.SelectedItems)
                {
                    String str = item["DanhMucTinTucID"].Text;
                    if (item.Selected == true)
                    {
                        DanhMucTinTucProvider.changePublish(Int32.Parse(str), false, IconTrangThai);
                    }
                    Count++;
                }
            }
            if (Count > 0)
                Response.Redirect("EHTCategoryNews.aspx?DanhMucID=" + danhmucID.ToString());
        }

        protected void ImgBat_Click(object sender, ImageClickEventArgs e)
        {
            String IconTrangThai = "~//images//tick.png";
            Int32 Count = 0;
            if (Session[HangSo.LANGUAGE_CODE].ToString() == "vi-VN")
            {
                foreach (TreeListDataItem item in RadTreeList1.SelectedItems)
                {
                    String str = item["DanhMucTinTucID"].Text;
                    if (item.Selected == true)
                    {
                        DanhMucTinTucProvider.changePublish(Int32.Parse(str), false, IconTrangThai);
                    }
                    Count++;
                }
            }
            else
            {

                foreach (TreeListDataItem item in RadTreeList2.SelectedItems)
                {
                    String str = item["DanhMucTinTucID"].Text;
                    if (item.Selected == true)
                    {
                        DanhMucTinTucProvider.changePublish(Int32.Parse(str), false, IconTrangThai);
                    }
                    Count++;
                }
            }
            if (Count > 0)
                Response.Redirect("EHTCategoryNews.aspx?DanhMucID=" + danhmucID.ToString());
        }

        protected void ImgDel_Click(object sender, ImageClickEventArgs e)
        {
            ArrayList arr = new ArrayList();
            if (Session[HangSo.LANGUAGE_CODE].ToString() == "vi-VN")
            {
                foreach (TreeListDataItem item in RadTreeList1.SelectedItems)
                {
                    String str = item["DanhMucTinTucID"].Text;
                    DataSet ds = DanhMucTinTucProvider.GetDanhMucTinTucsFillter(Int32.Parse(str), TenDanhMucTinTucVN.ToString(), TenDanhMucTinTucOther.ToString());
                    if (ds.Tables[0].Rows.Count > 1)
                    {
                        if (IsVN == true)
                            ltrErorr.Text = HelpMethod.ErorrLogin(HangSo.Del_Erorr);
                        else
                            ltrErorr.Text = HelpMethod.ErorrLogin(HangSo.Del_ErorrOther);
                        return;
                    }
                    else
                    {
                        arr.Add(str);
                    }

                }
            }

            else
            {
                foreach (TreeListDataItem item in RadTreeList2.SelectedItems)
                {
                    String str = item["DanhMucTinTucID"].Text;
                    DataSet ds = DanhMucTinTucProvider.GetDanhMucTinTucsFillter(Int32.Parse(str), TenDanhMucTinTucVN.ToString(), TenDanhMucTinTucOther.ToString());
                    if (ds.Tables[0].Rows.Count > 1)
                    {
                        if (IsVN == true)
                            ltrErorr.Text = HelpMethod.ErorrLogin(HangSo.Del_Erorr);
                        else
                            ltrErorr.Text = HelpMethod.ErorrLogin(HangSo.Del_ErorrOther);
                        return;
                    }
                    else
                    {
                        arr.Add(str);
                    }

                }
            }
            for (int i = 0; i < arr.Count; i++)
            {
                DanhMucTinTuc theDanhMuc = DanhMucTinTucProvider.GetDanhMucTinTucByDanhMucID(Int32.Parse(arr[i].ToString()));
                if (theDanhMuc.TypeDanhMuc != 2)
                    DanhMucTinTucProvider.DeleteDanhMucTinTucByDanhMucID(Int32.Parse(arr[i].ToString()));
                else
                {
                    if (IsVN == true)
                        ltrErorr.Text = "Bạn không thể xóa danh mục này";
                    else
                        ltrErorr.Text = "You can not delete";
                    return;
                }
            }

            if (arr.Count > 0)
                Response.Redirect("EHTCategoryNews.aspx?DanhMucID=" + danhmucID.ToString());
        }

        protected void ImgEdit_Click(object sender, ImageClickEventArgs e)
        {
            string ID = "";
            Int32 Count = 0;
            if (Session[HangSo.LANGUAGE_CODE].ToString() == "vi-VN")
            {
                foreach (TreeListDataItem item in RadTreeList1.SelectedItems)
                {
                    String str = item["DanhMucTinTucID"].Text;
                    DanhMucSanPham theDanhMucSp = DanhMucSanPhamProvider.GetDanhMucSanPhamByDanhMucID(Int32.Parse(str));
                    ID = str;
                    Count++;
                }
            }
            else
            {
                foreach (TreeListDataItem item in RadTreeList2.SelectedItems)
                {
                    String str = item["DanhMucTinTucID"].Text;
                    DanhMucSanPham theDanhMucSp = DanhMucSanPhamProvider.GetDanhMucSanPhamByDanhMucID(Int32.Parse(str));
                    ID = str;
                    Count++;
                }
            }
            if (Count > 1)
            {
                if (IsVN == true)
                    ltrErorr.Text = HelpMethod.ErorrLogin(HangSo.Edit_VN);
                else
                    ltrErorr.Text = HelpMethod.ErorrLogin(HangSo.Eorr_Add);
                return;
            }

            if (Count == 1)
                Response.Redirect("EHTCategoryNewsUI.aspx?DanhMucID=" + ID.ToString());
        }

        protected void ddlDanhmucSanPham_SelectedIndexChanged(object sender, EventArgs e)
        {
            Response.Redirect("EHTCategoryNews.aspx?DanhMucID="+ddlDanhmucSanPham.SelectedValue);
        }

        protected void ImagSearch_Click(object sender, ImageClickEventArgs e)
        {
            Response.Redirect("EHTCategoryNews.aspx?DanhMucID=" + danhmucID + "&&txtSearch="+txtsearch.Text);
        }

        [AjaxPro.AjaxMethod]
        protected void RadTreeList1_ItemCommand(object sender, TreeListCommandEventArgs e)
        {
            if (e.CommandName == RadTreeList.ExpandCollapseCommandName)
            {
                RadTreeList1.DataSource = DanhMucTinTucProvider.GetDanhMucTinTucsFillter(danhmucID, TenDanhMucTinTucVN, TenDanhMucTinTucOther);
                RadTreeList1.DataBind();
            }
        }

        [AjaxPro.AjaxMethod]
        protected void RadTreeList1_NeedDataSource(object sender, TreeListNeedDataSourceEventArgs e)
        {
            if (IsVN == true)
                TenDanhMucTinTucVN = Search;
            else
                TenDanhMucTinTucOther = Search;
            RadTreeList1.DataSource = DanhMucTinTucProvider.GetDanhMucTinTucsFillter(danhmucID, TenDanhMucTinTucVN, TenDanhMucTinTucOther);
        }

        [AjaxPro.AjaxMethod]
        protected void RadTreeList1_PageIndexChanged(object sender, TreeListPageChangedEventArgs e)
        {
            RadTreeList1.CurrentPageIndex = e.NewPageIndex;
            RadTreeList1.DataSource = DanhMucTinTucProvider.GetDanhMucTinTucsFillter(danhmucID, TenDanhMucTinTucVN, TenDanhMucTinTucOther);
            RadTreeList1.DataBind();
        }

        [AjaxPro.AjaxMethod]
        protected void RadTreeList1_PageSizeChanged(object sender, TreeListPageSizeChangedEventArgs e)
        {
            RadTreeList1.DataSource = DanhMucTinTucProvider.GetDanhMucTinTucsFillter(danhmucID, TenDanhMucTinTucVN, TenDanhMucTinTucOther);
            RadTreeList1.DataBind();
        }

        protected void ImagDichuyen_Click(object sender, ImageClickEventArgs e)
        {
            Session.Remove("ArrayListID");
            ArrayList arr = new ArrayList();
            if (Session[HangSo.LANGUAGE_CODE].ToString() == "vi-VN")
            {
                foreach (TreeListDataItem item in RadTreeList1.SelectedItems)
                {
                    String str = item["DanhMucTinTucID"].Text;
                    arr.Add(str);

                }
            }
            else
            {
                foreach (TreeListDataItem item in RadTreeList2.SelectedItems)
                {
                    String str = item["DanhMucTinTucID"].Text;
                    arr.Add(str);

                }
            }
            if (arr.Count > 0)
            {
                Session["ArrayListID"] = arr;
                Response.Redirect("EHTCategoryNewsCopy_Move.aspx?Type=Move");
            }
            else
                return;
        }

        protected void ImgSaochep_Click(object sender, ImageClickEventArgs e)
        {
            Session.Remove("ArrayListID");
            ArrayList arr = new ArrayList();
            if (Session[HangSo.LANGUAGE_CODE].ToString() == "vi-VN")
            {
                foreach (TreeListDataItem item in RadTreeList1.SelectedItems)
                {
                    String str = item["DanhMucTinTucID"].Text;
                    arr.Add(str);

                }
            }
            else
            {
                foreach (TreeListDataItem item in RadTreeList2.SelectedItems)
                {
                    String str = item["DanhMucTinTucID"].Text;
                    arr.Add(str);

                }
            }
            if (arr.Count > 0)
            {
                Session["ArrayListID"] = arr;
                Response.Redirect("EHTCategoryNewsCopy_Move.aspx?Type=Copy");
            }
            else
                return;
        }

        [AjaxPro.AjaxMethod]
        protected void RadTreeList2_ItemCommand(object sender, TreeListCommandEventArgs e)
        {
            if (e.CommandName == RadTreeList.ExpandCollapseCommandName)
            {
                RadTreeList2.DataSource = DanhMucTinTucProvider.GetDanhMucTinTucsFillter(danhmucID, TenDanhMucTinTucVN, TenDanhMucTinTucOther);
                RadTreeList2.DataBind();
            }
        }

        [AjaxPro.AjaxMethod]
        protected void RadTreeList2_NeedDataSource(object sender, TreeListNeedDataSourceEventArgs e)
        {
            if (IsVN == true)
                TenDanhMucTinTucVN = Search;
            else
                TenDanhMucTinTucOther = Search;
            RadTreeList2.DataSource = DanhMucTinTucProvider.GetDanhMucTinTucsFillter(danhmucID, TenDanhMucTinTucVN, TenDanhMucTinTucOther);
        }

        [AjaxPro.AjaxMethod]
        protected void RadTreeList2_PageIndexChanged(object sender, TreeListPageChangedEventArgs e)
        {
            RadTreeList2.CurrentPageIndex = e.NewPageIndex;
            RadTreeList2.DataSource = DanhMucTinTucProvider.GetDanhMucTinTucsFillter(danhmucID, TenDanhMucTinTucVN, TenDanhMucTinTucOther);
            RadTreeList2.DataBind();
        }

        [AjaxPro.AjaxMethod]
        protected void RadTreeList2_PageSizeChanged(object sender, TreeListPageSizeChangedEventArgs e)
        {
            RadTreeList2.DataSource = DanhMucTinTucProvider.GetDanhMucTinTucsFillter(danhmucID, TenDanhMucTinTucVN, TenDanhMucTinTucOther);
            RadTreeList2.DataBind();
        }

        protected void imgConfig_Click(object sender, ImageClickEventArgs e)
        {
            Session.Remove("ArrayListConfig");
            ArrayList arr = new ArrayList();
            if (Session[HangSo.LANGUAGE_CODE].ToString() == "vi-VN")
            {
                foreach (TreeListDataItem item in RadTreeList1.SelectedItems)
                {
                    String str = item["DanhMucTinTucID"].Text;
                    arr.Add(str);

                }
            }
            else
            {
                foreach (TreeListDataItem item in RadTreeList2.SelectedItems)
                {
                    String str = item["DanhMucTinTucID"].Text;
                    arr.Add(str);

                }
            }
            if (arr.Count > 0)
            {
                Session["ArrayListConfig"] = arr;
                Response.Redirect("EHTConfigCategoryNews.aspx");
            }
            else
                return;
        }
    }
}